我的版本: 超大的一个数组过不去
class Solution(object):
def threeSum\(self, nums\):
"""
:type nums: List\[int\]
:rtype: List\[List\[int\]\]
"""
result=\[\]
nums.sort\(\)
for i in range\(0,len\(nums\)-2\):
for j in range\(i+1,len\(nums\)-1\):
k=-nums\[i\]-nums\[j\]
if k in nums\[j+1:\] and \(\[nums\[i\],nums\[j\],k\] not in result\) and \(\[nums\[i\],k,nums\[j\]\] not in result \) and \(\[nums\[j\],nums\[i\],k\] not in result \) and \(\[nums\[j\],k,nums\[i\]\] not in result \)and \(\[k,nums\[i\],nums\[j\]\] not in result \) and \(\[k,nums\[j\],nums\[i\]\] not in result \):
result+=\[\[nums\[i\],nums\[j\],k\]\]
return result
class Solution(object):
def threeSum\(self, num\):
"""
:type nums: List\[int\]
:rtype: List\[List\[int\]\]
"""
res=\[\]
num.sort\(\)
for i in range\(0,len\(num\)-2\):
if i == 0 or num\[i\] > num\[i-1\]:
left=i+1
right=len\(num\)-1
while left < right:
if num\[left\] + num\[right\] == -num\[i\]:
res.append\(\[num\[i\], num\[left\], num\[right\]\]\)
left += 1; right -= 1
while left < right and num\[left\] == num\[left-1\]: left +=1
#去重
while left < right and num\[right\] == num\[right+1\]: right -= 1
#去重
elif num\[left\] + num\[right\] < -num\[i\]:
while left < right:
left += 1
if num\[left\] > num\[left-1\]: break
else:
while left < right:
right -= 1
if num\[right\] < num\[right+1\]: break
return res
这是网上找的
后面我用这个改了一下,发现如果中间没有那几个break还是不能过
好烦啊啊啊啊啊啊